/*
 * Copyright (C) 2007-2011  The Paparazzi Team
 *
 * This file is part of paparazzi.
 *
 * paparazzi is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * paparazzi is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with paparazzi; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/**
 * @file modules/nav/common_flight_plan.h
 * Common flight_plan functions shared between fixedwing and rotorcraft.
 */

#ifndef COMMON_FLIGHT_PLAN_H
#define COMMON_FLIGHT_PLAN_H

#include "std.h"

/** In s */
extern uint16_t stage_time, block_time;

/* The modification of the following variables do not affect
 * the behavior of the flight plan
 */
extern uint8_t nav_stage, nav_block;
extern uint8_t last_block, last_stage;
extern uint8_t last_wp __attribute__((unused));

/* init function */
extern void common_flight_plan_init(void);

/** needs to be implemented by fixedwing and rotorcraft seperately */
extern void nav_init_stage(void);

extern void nav_init_block(void);
extern void nav_goto_block(uint8_t block_id);
extern void nav_goto_next_block(void);
extern void nav_goto_next_stage(void);
extern void nav_return(uint8_t reset);

/* Getter */
extern uint8_t get_nav_block(void);
extern uint8_t get_nav_stage(void);
extern uint8_t get_last_block(void);
extern uint8_t get_last_stage(void);

/* Setter */
extern void set_nav_block(uint8_t block_id);
extern void set_nav_stage(uint8_t stage_id);

/** Functions generated by the flight plan */
extern void nav_on_enter_block(uint8_t block_id);
extern void nav_on_exit_block(uint8_t block_id);

#define InitStage() nav_init_stage();

#define Block(x) case x: set_nav_block(x);
#define NextBlock() nav_goto_next_block()
#define GotoBlock(b) nav_goto_block(b)

#define Stage(s) case s: set_nav_stage(s);
#define NextStage() { nav_goto_next_stage(); } INTENTIONAL_FALLTHRU
#define NextStageAndBreak() { nav_goto_next_stage(); break; }
#define NextStageAndBreakFrom(wp) { last_wp = wp; NextStageAndBreak(); }

#define Label(x) label_ ## x:
#define Goto(x) { goto label_ ## x; }
#define Return(x) nav_return(x)

/** Time in s since the entrance in the current block */
#define NavBlockTime() (block_time)

/** macro to use pointers in flight plans XML */
#define Ptr(x) (&(x))

#endif /* COMMON_FLIGHT_PLAN_H */

